package com.hangim.pce.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import com.hangim.pce.model.SysPrescriptionDetailRule;
import com.hangim.pce.service.SysPrescriptionDetailRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 规则触发处方详情表 前端控制器
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@RestController
@RequestMapping("/sysPrescriptionDetailRule")
public class SysPrescriptionDetailRuleController extends BaseController {
    @Autowired
    private SysPrescriptionDetailRuleService sysPrescriptionDetailRuleService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysPrescriptionDetailRule
     */
    @GetMapping("/{id}")
    public R<SysPrescriptionDetailRule> get(@PathVariable Long id) {
        return new R<>(sysPrescriptionDetailRuleService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysPrescriptionDetailRuleService.selectPage(new Query<>(params));
    }

    /**
     * 添加
     *
     * @param sysPrescriptionDetailRule 实体
     * @return success/false
     */
    @PostMapping
    public R<Integer> add(@RequestBody SysPrescriptionDetailRule sysPrescriptionDetailRule) {
        return new R<>(sysPrescriptionDetailRuleService.insert(sysPrescriptionDetailRule));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Integer> delete(@PathVariable Long id) {
        SysPrescriptionDetailRule sysPrescriptionDetailRule = new SysPrescriptionDetailRule();
        sysPrescriptionDetailRule.setId(id);
        sysPrescriptionDetailRule.setUpdatedAt(new Date());
        sysPrescriptionDetailRule.setActive(true);
        return new R<>(sysPrescriptionDetailRuleService.updateById(sysPrescriptionDetailRule));
    }

    /**
     * 编辑
     *
     * @param sysPrescriptionDetailRule 实体
     * @return success/false
     */
    @PutMapping
    public R<Integer> edit(@RequestBody SysPrescriptionDetailRule sysPrescriptionDetailRule) {
        sysPrescriptionDetailRule.setUpdatedAt(new Date());
        return new R<>(sysPrescriptionDetailRuleService.updateById(sysPrescriptionDetailRule));
    }
}
